import './index.css'
import {List, Image, AutoCenter, Button} from 'antd-mobile'
import React, {useContext, useState} from "react";
import moment from "moment";
import {DashboardData, DeviceInfo, repairInfo} from "../../Datasource";
import {useNavigate} from "react-router-dom";


const Account = () => {
    const userInfo: any = JSON.parse(window.sessionStorage.getItem('userInfo')!)
    const navigate = useNavigate()

    const exportExcel = () => {
        const ExportJsonExcel = require("js-export-excel");
        let option: any = {};
        option.fileName = moment(new Date()).format('YYYYMMDD');
        const tempData: { sheetData: any[]; sheetName: string; sheetFilter: string[]; sheetHeader: string[]; }[] = []
        DashboardData.map(a => {
            const temp: any = a.data
            temp.map((e: any) => {
                if (e.type === 1) {
                    e.typeText = '空调'
                } else {
                    e.typeText = '蓄电池'
                }
            })
        })
        DashboardData.map(e => {
            tempData.push({
                sheetData: e.data,
                sheetName: e.name,
                sheetFilter: ['name', 'machineRoom', 'district', 'typeText', 'supplyMode', 'capacity', 'productionTime', 'updatePerson', 'updateTime'],
                sheetHeader: ['设备名称', '机房名称', '所属区县', '设备类型', '送风方式', '额定容量', '出厂日期', '修改人', '修改时间'],
            })
        })
        const temp: any = repairInfo
        temp.map((e: any) => {
            if (e.type === 1) {
                e.typeText = '空调'
            } else {
                e.typeText = '蓄电池'
            }
        })
        tempData.push({
            sheetData: repairInfo,
            sheetName: '故障报修',
            sheetFilter: ['deviceName', 'machineRoom', 'typeText', 'describe', 'maintenancePerson', 'contact'],
            sheetHeader: ['设备名称', '机房名称', '设备类型', '基本问题描述', '报修人姓名', '联系方式',],
        })
        option.datas = tempData
        const toExcel = new ExportJsonExcel(option);
        toExcel.saveExcel();
    }

    if (userInfo) {
        return <div>
            <AutoCenter>
                <Image
                    src={userInfo.avatar}
                    width={200}
                    height={200}
                    fit='cover'
                    style={{borderRadius: 32, marginBottom: 30}}
                />
            </AutoCenter>
            <List mode='card'>
                <List.Item children={'姓名'} extra={userInfo.name}/>
                <List.Item children={'工作岗位'} extra={userInfo.post}/>
                <List.Item children={'单位'} extra={userInfo.unit}/>
                <List.Item children={'手机号'} extra={userInfo.phone}/>
                <List.Item children={'角色'} extra={userInfo.role ? '后台管理人员' : '使用人'}/>
            </List>
            {
                userInfo.role &&
                <div style={{margin: '0 12px', marginBottom: 12}}>
                    <Button block type='submit' color='primary' size='large' onClick={() => {
                        exportExcel()
                    }}>
                        导出
                    </Button>
                </div>
            }
        </div>
    } else {
        return null
    }
}
export default Account
